﻿using mars_server.csharp.AO;
using mars_server.csharp.AO.impl;
using mars_server.csharp.CO;
using mars_server.csharp.dao;
using mars_server.csharp.dao.impl;
using mars_server.csharp.enums;
using mars_server.csharp.model;
using mars_server.csharp.socket;
using mars_server.csharp.Util;
using mars_server.csharp.VO;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace mars_server.csharp.controller
{
    class GroupController
    {
        private GroupController() { }
        private static readonly GroupController instance = new GroupController();

        internal static GroupController Instance
        {
            get
            {
                return instance;
            }
        }

        GroupAO groupAO = GroupAOImpl.Instance;

        UserGroupAO userGroupAO = UserGroupAOImpl.Instance;

        UserSocketAO userSocketAO = UserSocketAOImpl.Instance;

        GroupManager groupManager = GroupManagerImpl.Instance;

        //创建群操作
        public void createGroup(MessageCO messageCO, UserSocket userSocket)
        {
            GroupVO groupVO = JsonUtil.j2o<GroupVO>(messageCO.content);
            if (userSocket == null || userSocket.userDO == null || groupVO == null)
            {
                return;
            }
            List<UserVO> munberVOs = groupVO.userVOs;
            List<long> munberIds = new List<long>();
            foreach(UserVO munberVO in munberVOs)
            {
                munberIds.Add(munberVO.id);
            }
            if(munberIds.Count == 0)//创建指令中没有用户
            {
                return;
            }
            GroupDO groupDO = new GroupDO();
            groupDO.groupName = groupVO.groupName;
            groupDO.groupIntro = groupVO.groupIntro;

            groupAO.createGroupByGroupDO(groupDO);//创建群
            Console.WriteLine("[info]|--【创建群】群：" + groupVO.groupName);
            long groupId = userGroupAO.addUserGroupDO(groupDO.groupName, munberIds);//将用户加入群中
            if(groupId == 0)
            {
                return;
            }
            groupVO.id = groupId;
            messageCO.content = groupVO;
            userSocketAO.sendMessageToSocket(userSocket,messageCO);

        }

        //获取群用户信息
        public void queryGroupInfo(MessageCO messageCO, UserSocket userSocket)
        {
            GroupVO groupVO = JsonUtil.j2o<GroupVO>(messageCO.content);
            if (groupVO == null) 
            {
                return;
            }
            long GroupId = groupVO.id;
            GroupDO groupDO = groupManager.selectByPrimaryKey(GroupId);
            groupVO.groupName = groupDO.groupName;
            groupVO.groupIntro = groupDO.groupIntro;
            groupVO.id = groupDO.id;

            List<UserDO> userDOs = userGroupAO.queryUserDOsByGroupId(GroupId);
            List<UserVO> userVOs = new List<UserVO>();
            foreach(UserDO userDO in userDOs)
            {
               
                UserVO userVO = new UserVO();
                userVO = DOVOutil.userDO2VO(userDO);
                if (userSocketAO.isUserOnlie(userDO.id))
                {
                    userVO.isOnline =(int)IsOnlineEnum.online.Online;
                }
                userVOs.Add(userVO);
            }
            groupVO.userVOs = userVOs;
            messageCO.content = groupVO;
            userSocketAO.sendMessageToSocket(userSocket, messageCO);
            Console.WriteLine("[info]|--向" + SocketUtil.getSocketInfo(userSocket) + "发送消息成功");
        }

        //获取用户的群列表
        public void getGroupList(MessageCO messageCO, UserSocket userSocket)
        {
            if(userSocket.userDO == null)
            {
                return;
            }
            List<long> groupIds = userGroupAO.getGroupIdsByUserId(userSocket.userDO.id);
            if(groupIds.Count == 0)
            {
                return;
            }
            List<GroupDO> groupDOs =  groupManager.selectGroupDOsByGroupIds(groupIds);
            List<GroupVO> groupVOs = new List<GroupVO>();
            foreach(GroupDO groupDO in groupDOs)
            {
                GroupVO groupVO = new GroupVO();
                groupVO.id = groupDO.id;
                groupVO.groupName = groupDO.groupName;
                groupVO.groupIntro = groupDO.groupIntro;
                groupVO.createTime = groupDO.gmtGreate;
                groupVOs.Add(groupVO);
            }
            GroupListVO groupListVO = new GroupListVO();
            groupListVO.groupVOs = groupVOs;
            messageCO.content = groupListVO;
            userSocketAO.sendMessageToSocket(userSocket, messageCO);
        }

    }
}
